package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.IConsumerMapper;
import com.goods.business.service.IConsumerService;
import com.goods.common.error.SystemCodeEnum;
import com.goods.common.error.SystemException;
import com.goods.common.model.business.Consumer;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.system.PageVO;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA
 *
 * @Date: 2023/3/6 - 17:59
 * @Author: Hyoung
 * @Description:
 */
@Service
public class ConsumerServiceImpl implements IConsumerService {
    @Autowired
    private IConsumerMapper iConsumerMapper;

    @Override
    public void addConsumer(ConsumerVO consumerVO) {
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerVO, consumer);
        consumer.setCreateTime(new Date());
        consumer.setModifiedTime(new Date());
        iConsumerMapper.insert(consumer);
    }

    @Override
    @SneakyThrows
    public void updateConsumer(Integer id, ConsumerVO consumerVO) {
        Consumer consumer = iConsumerMapper.selectByPrimaryKey(id);
        if (Objects.isNull(consumer)) {
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR, "没有该数据");
        }
        System.err.println(consumerVO);
        Consumer consumerSave = new Consumer();
        BeanUtils.copyProperties(consumerVO, consumerSave);
        consumer.setId(consumer.getId());
        consumer.setModifiedTime(new Date());
        iConsumerMapper.updateByPrimaryKey(consumerSave);
    }

    @Override
    public ConsumerVO findConsumerById(Integer id) {
        Consumer consumer = iConsumerMapper.selectByPrimaryKey(id);
        ConsumerVO consumerVO = new ConsumerVO();
        BeanUtils.copyProperties(consumer, consumerVO);
        return consumerVO;
    }

    @Override
    public void delete(Integer id) {
        iConsumerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageVO<ConsumerVO> findConsumerList(Integer pageNum, Integer pageSize, ConsumerVO consumerVO) {
        PageHelper.startPage(pageNum, pageSize);
        // name=fsda&address=fds&contact=fdsa
        String name = consumerVO.getName();
        String address = consumerVO.getAddress();
        String contact = consumerVO.getContact();
        Example exampleConsumer = new Example(Consumer.class);
        Example.Criteria criteria = exampleConsumer.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.andLike("name", "%" + name + "%");
        }
        if (!StringUtils.isEmpty(address)) {
            criteria.andLike("address", "%" + address + "%");
        }
        if (!StringUtils.isEmpty(contact)) {
            criteria.andLike("contact", "%" + contact + "%");
        }
        List<Consumer> consumerList = iConsumerMapper.selectByExample(exampleConsumer);
        List<ConsumerVO> collect = consumerList.stream().map(consumer -> {
            ConsumerVO consumerVOTemp = new ConsumerVO();
            BeanUtils.copyProperties(consumer, consumerVOTemp);
            return consumerVOTemp;
        }).collect(Collectors.toList());
        PageInfo<Consumer> consumerPageInfo = new PageInfo<>(consumerList);
        return new PageVO<>(consumerPageInfo.getTotal(), collect);
    }

    @Override
    public List<ConsumerVO> findAllConsumer() {

        List<Consumer> consumers = iConsumerMapper.selectAll();
        List<ConsumerVO> collect = consumers.stream().map(consumer -> {
            ConsumerVO consumerVO = new ConsumerVO();
            BeanUtils.copyProperties(consumer, consumerVO);
            return consumerVO;
        }).collect(Collectors.toList());
        return collect;
    }
}
